﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Telerik.Web.UI;

namespace Guru.Web.Controls.Validators
{
    public static class jQueryConvert
    {
        #region const
        public const string TAG_OPEN = "[OPEN]";
        public const string TAG_CLOSE = "[CLOSE]";
        public const string RADCOMBO_DOM_POSTFIX = "_Input";
        public const string RADUPLOAD_DOM_POSTFIX = "file0";
        public const string RADLISTBOX_DOM_POSTFIX = "_ClientState";
        #endregion

        /// <summary>
        /// return boolean string in jQuery
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToBoolean(this bool value)
        {
            return value ? "true" : "false";
        }

        /// <summary>
        /// Get jQuery rule's name from C# rule
        /// </summary>
        /// <param name="rule"></param>
        /// <returns></returns>
        public static string ToRuleName(this BasicValidationItem item)
        {
            if (item.Rule == ValidationRule.None)
                throw new PropertyNullRefException("BasicValidationItem.Rule");
            string ruleName = string.Empty;
            switch (item.Rule)
            {
                case ValidationRule.Required:
                    if (item.ObjectControlToValidate is RadListBox) {
                        ruleName= "radlistboxrequire";
                    }
                    else ruleName = "required";
                    break;
                case ValidationRule.Email:
                    ruleName = "email";
                    break;
                case ValidationRule.Url:
                    ruleName = "url";
                    break;
                case ValidationRule.Digits:
                    ruleName = "digits";
                    break;
                case ValidationRule.Number:
                    ruleName = "number";
                    break;
                case ValidationRule.AlphaNumeric:
                    ruleName = "alphanumeric";
                    break;
                case ValidationRule.Integer:
                    ruleName = "integer";
                    break;
                case ValidationRule.LettersOnly:
                    ruleName = "lettersonly";
                    break;
                case ValidationRule.NoWhiteSpace:
                    ruleName = "nowhitespace";
                    break;
                case ValidationRule.Minlength:
                    ruleName = "minlength";
                    break;
                case ValidationRule.Maxlength:
                    ruleName = "maxlength";
                    break;
                case ValidationRule.Rangelength:
                    ruleName = "rangelength";
                    break;
                case ValidationRule.GreaterOrEqualThan:
                    ruleName = "min";
                    break;
                case ValidationRule.LessOrEqualThan:
                    ruleName = "max";
                    break;
                case ValidationRule.Range:
                    ruleName = "range";
                    break;
                case ValidationRule.Accept:
                    ruleName = "accept";
                    break;
                case ValidationRule.StringAccept:
                    ruleName = "stringaccept";
                    break;
                case ValidationRule.StringUnaccept:
                    ruleName = "stringunaccept";
                    break;
                case ValidationRule.GreaterThan:
                    ruleName = "greaterthan";
                    break;
                case ValidationRule.LessThan:
                    ruleName = "lessthan";
                    break;
                case ValidationRule.ListBoxHasItem:
                    if (item.ObjectControlToValidate is RadListBox)                    
                        ruleName = "radlistboxhasitem";                    
                    else
                        ruleName = "listboxhasitem";
                    break;
                case ValidationRule.RadComboValueRequired:
                    ruleName = "RadComboValueRequired";
                    break;
            }
            return ruleName;
        }

        /// <summary>
        /// Get value for the rule
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string ToRuleValue(this BasicValidationItem item)
        {
            string ruleValue = string.Empty;
            switch (item.Rule)
            { 
                case ValidationRule.Required:
                case ValidationRule.ListBoxHasItem:
                    if (item.ObjectControlToValidate is RadListBox)
                        ruleValue = string.Format("'{0}'", item.ObjectControlToValidate.ClientID);   
                    else 
                        ruleValue = "true";
                    break;
                case ValidationRule.Email:
                case ValidationRule.Url:
                case ValidationRule.Digits:
                case ValidationRule.Number:
                case ValidationRule.AlphaNumeric:
                case ValidationRule.Integer:
                case ValidationRule.LettersOnly:
                case ValidationRule.NoWhiteSpace:               
                    ruleValue = "true";
                    break;
                case ValidationRule.Minlength:
                    ruleValue = item.MinLength.ToString();
                    break;
                case ValidationRule.Maxlength:
                    ruleValue = item.MaxLength.ToString();
                    break;
                case ValidationRule.Rangelength:
                    ruleValue = string.Format("[{0}, {1}]", item.MinLength, item.MaxLength);
                    break;
                case ValidationRule.GreaterOrEqualThan:
                case ValidationRule.GreaterThan:
                    ruleValue = item.MinValue.ToString();
                    break;
                case ValidationRule.LessOrEqualThan:
                case ValidationRule.LessThan:
                    ruleValue = item.MaxValue.ToString();
                    break;

                case ValidationRule.Range:
                    ruleValue = string.Format("[{0}, {1}]", item.MinValue, item.MaxValue);
                    break;
                case ValidationRule.Accept:
                    ruleValue = string.Format("\"{0}\"", item.Extensions);
                    break;
                case ValidationRule.StringAccept:
                case ValidationRule.StringUnaccept:
                    ruleValue = string.Format("\"{0}\"", item.Pattern);
                    break;
                case ValidationRule.RadComboValueRequired:
                    ruleValue = string.Format("'{0}'", item.ObjectControlToValidate.ClientID);   
                    break;
            }
            return ruleValue;
        }

        /// <summary>
        /// Replace [OPEN], [CLOSE] by {,}
        /// </summary>
        /// <param name="orginalText"></param>
        /// <returns></returns>
        public static string ReplaceTag(this string orginalText)
        {
            return orginalText.Replace(TAG_OPEN, "{").Replace(TAG_CLOSE, "}");
        }

        /// <summary>
        /// Get DOM id of the control
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public static string ToJQueryId(this Control control)
        {
            if (control is RadComboBox)
                return string.Format("{0}{1}", control.ClientID, RADCOMBO_DOM_POSTFIX);
            else if(control is RadUpload)
                return string.Format("{0}{1}", control.ClientID, RADUPLOAD_DOM_POSTFIX);
            else if (control is RadListBox)
                return string.Format("{0}{1}", control.ClientID, RADLISTBOX_DOM_POSTFIX);
            return control.ClientID;
        }


    }
}
